package sharm.service.impl;

import sharm.dao.impl.CommentDaoImpl;
import sharm.pojo.Book;
import sharm.pojo.Comment;
import sharm.pojo.Page;
import sharm.service.CommentService;

import java.util.List;

/**
 * @author ViTe
 * @create 2022/6/4 - 19:01
 */
public class CommentServiceImpl implements CommentService {
    private CommentDaoImpl commentDao = new CommentDaoImpl();

    @Override
    public void addComment(Comment comment) {
        commentDao.addComment(comment);
    }

    @Override
    public void deleteCommentByid(Integer id) {
        commentDao.deleteCommentByid(id);
    }

    @Override
    public List<Comment> queryComment(String gamename) {
        return commentDao.queryComments(gamename);
    }

    @Override
    public Page<Comment> page(int pageNo, int pageSize, String gamename) {
        Page<Comment> page = new Page<Comment>();

        // 设置每页显示的数量
        page.setPageSize(pageSize);
        // 求总记录数
        Integer pageTotalCount = commentDao.queryForPageTotalCount(gamename);
        // 设置总记录数
        page.setPageTotalCount(pageTotalCount);
        // 求总页码
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal+=1;
        }
        // 设置总页码
        page.setPageTotal(pageTotal);

        // 设置当前页码
        page.setPageNo(pageNo);

        // 求当前页数据的开始索引
        int begin = (page.getPageNo() - 1) * pageSize;

        // 求当前页数据
//        System.out.println(gamename);
        List<Comment> items = commentDao.queryForPageItems(begin, pageSize, gamename);
//        for (Comment c: items
//             ) {
//            System.out.println(c.getName());
//        }
        // 设置当前页数据
        page.setItems(items);
        return page;
    }
}
